<template>
  <div class="app-container">
    <el-form :model="dataForm" status-icon :rules="rules" label-position="left" label-width="110px"
      style="width: 700px; margin-left:50px;">
      <el-form-item :label='$t("会员名称")' prop="name">
        <el-input v-model="dataForm.name" maxlength="64" clearable style="width:600px" />
      </el-form-item>
      <el-form-item :label='$t("会员图片")' prop="picUrl">
        <el-upload :headers="headers" :action="uploadPath" :show-file-list="false" :on-success="uploadPicUrl"
          :on-remove="handleGoodsPicRemove" class="avatar-uploader" accept=".jpg,.jpeg,.png,.gif">
          <img v-if="dataForm.picUrl" :src="dataForm.picUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon" />
          <label v-if="dataForm.picUrl" class="el-upload-list__item-status-label avatar-uploader__status_label"
            @click.stop="handleGoodsPicRemove()"><i class="el-icon-delete"></i></label>
        </el-upload>
      </el-form-item>
      <el-form-item :label='$t("使用范围")' prop="useRange">
        <el-radio v-model="dataForm.useRange" :label="1">{{ $t("门店使用") }}</el-radio>
        <el-radio v-model="dataForm.useRange" :label="2">{{ $t("网店使用") }}</el-radio>
        <el-radio v-model="dataForm.useRange" :label="3">{{ $t("通用") }}</el-radio>
      </el-form-item>
      <el-form-item :label='$t("限制区域")'>
        <el-radio-group v-model="areaLimit">
          <el-radio-button :label="0">{{ $t("不限区域") }}</el-radio-button>
          <el-radio-button :label="1">{{ $t("限制区域") }}</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-show="areaLimit == 0 ? false : true">
        <el-cascader :options="locations" :props="regionProps" v-model="dataForm.shopIds" :show-all-levels="false"
          collapse-tags clearable>
        </el-cascader>
      </el-form-item>
      <!-- <el-form-item :label='$t("有效期")' prop="expiredDays">
        <el-date-picker v-model="value1" type="daterange" range-separator="至" start-placeholder="开始日期"
          end-placeholder="结束日期" @change="changeDatePick" style="width:380px" value-format="yyyy-MM-dd HH:mm:ss">
        </el-date-picker>
        {{ $t('有效天数') }}:<el-input style="width:100px" v-model="dataForm.expiredDays" @input="inputExpiredDays" />
      </el-form-item> -->
      <el-form-item :label='$t("有效期")' prop="expiredDays">
        <el-radio-group v-model="radio">
          <el-radio :label="0">{{$t("时间范围")}}</el-radio>
          <el-radio :label="1">{{$t("有效天数")}}</el-radio>
        </el-radio-group>
        <div v-show="radio == 0">
          <el-date-picker v-model="value1" type="daterange" :range-separator='$t("至")' :start-placeholder='$t("开始日期")'
            :end-placeholder='$t("结束日期")' @change="changeDatePick" style="width:380px" value-format="yyyy-MM-dd HH:mm:ss">
          </el-date-picker>
        </div>
        <div v-show="radio == 1">
          {{ $t('有效天数') }}:<el-input style="width:100px" v-model="expiredDays" @change="inputDays" type="number"
            min="1" />
        </div>
      </el-form-item>
      <el-form-item :label='$t("可购买次数")'>
        <el-radio-group v-model="buyTimeType">
          <el-radio :label="0">
            {{ $t('一次性') }}
          </el-radio>

          <el-radio :label="1">
            {{ $t('无限次数') }}
          </el-radio>

          <el-radio :label="2">
            {{ $t('自定义次数') }}
            <el-input style="width:100px" v-model="dataForm.buyTimes" :min="1" :disabled="buyTimeType != 2" clearable />
          </el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item :label='$t("购买金额")' prop="point">

        <el-input style="width:100px" clearable v-model="dataForm.money" />
        $ or
        <el-input v-model="dataForm.point" style="width:100px" clearable />
        {{ $t('积分') }}
      </el-form-item>
      <el-form-item :label='$t("是否支持续订")' prop="isSupportReneval">
        <el-checkbox v-model="dataForm.isSupportReneval">{{ $t("是") }}</el-checkbox>
        <!-- <el-checkbox-group v-model="dataForm.isSupportReneval">
           <el-checkbox  :label="1">{{$t("是")}}</el-checkbox>
         </el-checkbox-group> -->
      </el-form-item>
      <el-form-item :label='$t("续约金额")' prop="renevalPoint  ">
        <el-input :disabled="dataForm.isSupportReneval == false" style="width:100px" v-model="dataForm.renevalMoney"
          clearable />
        $ or
        <el-input :disabled="dataForm.isSupportReneval == false" v-model="dataForm.renevalPoint" style="width:100px"
          clearable />
        {{ $t('积分') }}

      </el-form-item>
      <hr>
      <h4>{{ $t("专享特权") }}</h4>
      <el-form-item :label='$t("积分获取比例")' prop="pointRatio">
        {{ $t('消费') }} 1 {{ $t('元') }} =
        <el-input style="width:100px" v-model="dataForm.pointRatio" clearable />
        {{ $t('积分') }}
      </el-form-item>
      <el-form-item :label='$t("会员消费")' prop="dicountPer">
        <el-radio-group v-model="dicountPer" @change="RadioChange">
          <el-radio :label="0">{{ $t("无") }}</el-radio>
          <div></div>
          <el-radio :label="1">
            <el-input v-model="dataForm.dicountPer" @focus="InputFoucs(dicountPer = 1)" clearable />
            %{{ $t('优惠') }}
          </el-radio>
          <div></div>
        </el-radio-group>
        <el-checkbox-group v-model="checkList" @change="changeOverLay">
          <el-checkbox :label="1">{{ $t('会员是否优惠叠加') }}</el-checkbox>
        </el-checkbox-group>
        <!-- <el-radio-group  v-model="dataForm.is_overlay">
          <el-radio :label="1" @change="changeOverLay"></el-radio>
        </el-radio-group> -->
      </el-form-item>

      <el-form-item :label='$t("付费说明")' prop="paymentInstructions">
        <el-input type="textarea" v-model="dataForm.paymentInstructions" maxlength="64" clearable />
      </el-form-item>

      <hr style="color:#bbb">
      <div class='TipsTitle'>{{ $t("专享福利") }}</div>
      <div class='TopAdd'>
        <div></div>
        <el-button class="filter-item" type="primary" icon="el-icon-edit" @click="handleAttributeShow">{{ $t('Add') }}
        </el-button>
      </div>
      <el-table border :data="this.list">
        <el-table-column :label='$t("名称")' align="center" prop="name"></el-table-column>
        <el-table-column :label='$t("数量")' prop="total" align="center"></el-table-column>
        <el-table-column align="center" :label='$t("类型")' prop="discountType">
          <template slot-scope="scope">
            {{scope.row.discountType}}
            <el-tag>
              {{ $t(scope.row.discountType == 1 ? '订阅' : '礼物卡') }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" :label="$t('Operate')" width="80" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button type="danger" size="mini" @click="handleDelete(scope.$index, scope.row)">{{ $t('Delete') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog :visible.sync="attributeVisiable" title="添加/设置福利">
        <el-form :model="attributeForm" status-icon label-position="left" label-width="100px"
          style=" margin-left:50px;">
          <el-form-item :label='$t("选择类型")' prop="discountType">
            <el-radio-group v-model="attributeForm.discountType" @change="RadioChanges(attributeForm.discountType)">
              <el-radio v-model="attributeForm.discountType" :label="1">{{ $t("订阅") }}</el-radio>
              <el-radio v-model="attributeForm.discountType" :label="2">{{ $t("礼物卡") }}</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item :label='$t("礼物卡")' prop="attribute" v-if="attributeForm.discountType == 2">
            <el-select v-model="attributeForm.gid" :placeholder='$t("无")'>
              <span v-for="(item)  in this.listGiftCard" v-bind:key="item.id">
                <el-option :label="item.name" :value="item.id"></el-option>
              </span>
            </el-select>
          </el-form-item>
          <el-form-item :label='$t("订阅")' prop="attributeForm.subscribeId" v-if="attributeForm.discountType == 1">
            <el-select v-model="attributeForm.subscribeId" :placeholder="无"
              @change="SelectChange(attributeForm.subscribeId)">
              <span v-for="(item)  in this.ListSubscribe" v-bind:key="item.id">
                <el-option :label="item.name" :value="item.id"></el-option>
              </span>
            </el-select>
          </el-form-item>
          <el-form-item :label='$t("发放周期")' prop="frequency" v-if="attributeForm.discountType == 0">
            <el-radio v-model="attributeForm.frequency" :label="1">{{ $t("每天") }}</el-radio>
            <el-radio v-model="attributeForm.frequency" :label="2">{{ $t("每周") }}</el-radio>
            <div></div>
            <el-radio v-model="attributeForm.frequency" :label="3">{{ $t("每月") }}</el-radio>
            <el-radio v-model="attributeForm.frequency" :label="4">{{ $t("每年") }}</el-radio>
          </el-form-item>
          <el-form-item :label='$t("订阅方式")' prop="frequency" v-if="attributeForm.discountType == 1">
            <span v-for="item in rule" v-bind:key="item" prop="rules">
              <el-radio v-model="attributeForm.rules" :label="item.id">{{ formateSubscribeRule(item) }}</el-radio>
              <div></div>
            </span>
          </el-form-item>
          <el-form-item :label='$t("数量")' prop="total" v-if="attributeForm.discountType == 0">
            <el-input :placeholder='$t("请填写数量")' v-model="attributeForm.total" style="width:202px" />
          </el-form-item>
        </el-form>
        <el-button style="width:150px" type="primary" @click="handleAttributeAdd">{{ $t('Confirm') }}</el-button>
      </el-dialog>
    </el-form>
    <div class="op-container">
      <el-button @click="handleBack" style="margin-right: 480px">{{ $t("返回") }}</el-button>
      <el-button type="primary" v-if="this.status == false" @click="updateData"
        v-permission="['GET /admin/p/member_pay_add']">{{ $t('Confirm') }}</el-button>
    </div>
  </div>
</template>

<script>
import { getToken } from '../../utils/auth'
import { uploadPath } from '../../api/storage'

import { listCoupon, listInMember } from '../../api/coupon'
import { ShopShopLocations, payMemberAdd, payMemberDelete, payMemberUpdate } from '@/api/user'
import { listLumiereSubscribe } from '../../api/lumiereSubscribe'
import { listGiftCard } from '../../api/gift'

export default {
  name: 'addedPaidUser',
  data() {
    // const validateDays = (rule, value, callback) => {
    //   if (this.dataForm.startTime && this.dataForm.endTime) {
    //     callback();
    //     return;
    //   }
    //   else if (this.days) {
    //     callback();
    //   }
    //   else {
    //     return callback(new Error(this.$t('有效期不能为空')));
    //   }
    // }
    return {
      expiredDays: undefined,
      radio: 0,
      dicountPer: 0,
      areaLimit: 0,
      paymentInstructions: undefined,
      attributeVisiable: false,
      attributeForm: {
        frequency: 1,
        discountType: 0,
        total: ''
      },
      isLevelMember: false,
      isMoneyMember: false,
      uploadPath,
      rules: {
        name: [{ required: true, message: this.$t('会员名称必填'), trigger: 'blur' }],
        // expiredDays: [{ required: true, message: this.$t('有效期不能为空'), trigger: 'blur' }],
        pointRatio: [{ required: true, message: this.$t('积分获取比例不能为空'), trigger: 'blur' }],
        // expiredDays: [
        //   { validator: validateDays, trigger: 'blur' }
        // ],
      },
      dataForm: {
        is_overlay: 0,
        id: undefined,
        name: undefined,
        picUrl: '',
        useRange: 1,
        buyTimes: 0,
        isSupportReneval: false,

      },
      value1: 0,
      buyTimeType: 0,
      attributeFormTotal: 0,
      dialogFormVisible: false,
      dialogStatus: '',
      list: [],
      categories: [],
      locations: [],
      goodsList: [],
      props: { multiple: true, value: 'id', label: 'name', emitPath: false },
      regionProps: { multiple: true, value: 'id', label: 'nameEn', emitPath: false },
      options: [{
        value: 0,
        label: '每年'
      }, {
        value: 1,
        label: '每月'
      }, {
        value: 2,
        label: '每周'
      }],
      checkList: [],
      goodsOnSale: [],
      specificationList: [],
      specificationQuery: {},
      listCoupon: [],
      listGiftCard: [],
      coupon: [],
      giftCard: [],
      couponDtos: [],
      giftCardVos: [],
      ListSubscribe: [],
      Subscribe: [],
      subscribeVos: [],
      status: undefined
    }
  },
  async created() {
    if (this.$route.query.status == 2) {
      this.status = true;
    } else {
      this.status = false;
    }
    await this.GetListGiftCard()
    await this.GetListLumiereSubscribe()
    await this.GetListCoupon()
    await this.getLocations()
    if (this.$route.query.type == 2) {
      await this.getGoods(this.$route.query.id)
      this.type = this.$route.query.type
      this.id = this.$route.query.id
    }
    if (this.dataForm.startTime) {
      this.value1 = [this.dataForm.startTime, this.dataForm.endTime];
      this.radio = 0;
    } else {
      this.expiredDays = this.dataForm.expiredDays;
      this.radio = 1;
    }

  },
  methods: {
    inputDays(val) {
      this.dataForm.expiredDays = val;
      this.value1 = undefined;
      this.dataForm.startTime = undefined;
      this.dataForm.endTime = undefined;
    },
    getNextDate(date, day = 1, format = "{y}-{m}-{d}") {
      if (date) {
        const nDate = new Date(date);
        nDate.setDate(nDate.getDate() + day);

        const formatObj = {
          y: nDate.getFullYear(),
          m: nDate.getMonth() + 1,
          d: nDate.getDate(),
        };
        return format.replace(/{([ymd])+}/g, (result, key) => {
          const value = formatObj[key];
          return value.toString().padStart(2, "0");
        });
      } else {
        throw new Error("getNextDate:错误的参数");
      }
    },
    inputExpiredDays(val) {
      const now = new Date();
      const endTime = this.getNextDate(now.toJSON().substr(0, 10), +val) + ' 00:00:00';
      const startTime = now.toJSON().substr(0, 10) + ' 00:00:00';
      this.value1 = [startTime, endTime];
    },
    changeOverLay(val) {
      // console.log(val,$event);
      this.dataForm.is_overlay = val.filter(item => item != 0)[0] || 0;
      this.dicountPer = 1;
    },
    // changeDatePick(val) {
    //   if (val) {
    //     const [start, end] = val;
    //     console.log(val);
    //     const date1 = new Date(start).getTime();
    //     const date2 = new Date(end).getTime();
    //     let day = Math.abs(date1 - date2);
    //     day = day / (1000 * 60 * 60 * 24);
    //     this.dataForm.startTime = start;
    //     this.dataForm.endTime = end;
    //     this.$set(this.dataForm, 'expiredDays', day);
    //   } else {
    //     this.dataForm.startTime = undefined;
    //     this.dataForm.endTime = undefined;
    //     this.dataForm.expiredDays = undefined;
    //   }
    // },
    changeDatePick(val) {
      if (val) {
        const [start, end] = val;
        this.dataForm.startTime = start;
        this.dataForm.endTime = end;
        const date1 = new Date(start).getTime();
        const date2 = new Date(end).getTime();
        let day = Math.abs(date1 - date2);
        day = day / (1000 * 60 * 60 * 24);
        this.$set(this.dataForm, 'expiredDays', day);
      } else {
        this.dataForm.startTime = undefined;
        this.dataForm.endTime = undefined;
      }
      this.expiredDays = undefined;
    },
    changeAttributeFormTotal(obj) {
      const total = this.listCoupon.find(row => {
        return row.id == obj;
      }).total;
      this.attributeFormTotal = total;
    },
    getLocations() {
      return ShopShopLocations().then(response => {
        this.locations = response.data.data.list
        if (this.dataForm.shopIds && this.dataForm.shopIds.length > 0) {
          let p = []
          this.dataForm.shopIds.forEach(id => {
            this.locations.forEach(region => {
              if (region.children && region.children.length > 0) {
                region.children.forEach(item => {
                  if (item.children && item.children.length > 0) {
                    item.children.forEach(sub => {
                      if (sub.id == id) {
                        let tmp = sub.id
                        p.push(tmp)
                      }
                    })
                  }
                })
              }
            })
          })

        }
      })

    },
    GetListCoupon() {
      var data = {
        type: 1, sort: 'add_time', order: 'asc', page: 1,
        limit: 9999, status: 1, isExpired: true
      }
      return listInMember(data).then(res => {
        this.listCoupon = res.data.data.list
        this.coupon = res.data.data.list
      })
    },
    GetListGiftCard() {
      var data = {
        sort: 'add_time', order: 'desc', page: 1,
        limit: 9999, type: 1, status: 1,
      }
      return listGiftCard(data).then(res => {
        this.listGiftCard = res.data.data.list
        this.giftCard = res.data.data.list
      })
    },
    GetListLumiereSubscribe() {
      var data = {
        status: 1, sort: 'add_time', order: 'desc', page: 1,
        limit: 9999
      }
      return listLumiereSubscribe(data).then(res => {
        this.ListSubscribe = res.data.data.list
      })
    },
    RadioChanges(row) {
      this.attributeForm = {
        frequency: 1,
        discountType: row,
        pid: undefined,
        total: ''
      }
    },
    handleAttributeShow() {
      this.attributeForm = {
        frequency: 1,
        discountType: 0,
        pid: undefined,
        total: ''
      }
      this.rule = []
      this.listCoupon = this.coupon
      this.listGiftCard = this.giftCard
      for (var i = 0; i < this.couponDtos.length; i++) {
        this.listCoupon = this.listCoupon.filter(item => item.id != this.couponDtos[i].couponId)
      }
      for (var i = 0; i < this.subscribeVos.length; i++) {
        this.ListSubscribe = this.ListSubscribe.filter(item => item.id != this.subscribeVos[i].subscribeId)
      }

      for (var i = 0; i < this.giftCardVos.length; i++) {

        this.listGiftCard = this.listGiftCard.filter(item => item.id != this.giftCardVos[i].giftCardId)
      }

      this.attributeVisiable = true
    },
    handleAttributeAdd() {
      if (this.attributeForm.discountType == 0) {
        if (this.attributeForm.pid == undefined) {
          this.$notify.error({
            title: this.$t('Failed'),
            message: this.$t('请填选择商品')
          })
          return
        }
        if (this.attributeForm.total == '' || this.attributeForm.total <= 0) {
          this.$notify.error({
            title: this.$t('Failed'),
            message: this.$t('请填写正确数量')
          })
          return
        }
        if (this.attributeForm.total > this.attributeFormTotal) {
          this.$notify.error({
            title: this.$t('Failed'),
            message: this.$t('超出库存量')
          })
          return
        }
        for (let i = 0; i < this.listCoupon.length; i++) {
          if (this.listCoupon[i].id == this.attributeForm.pid) {
            this.attributeForm.name = this.listCoupon[i].name
          }
        }
        var data = {
          name: this.attributeForm.name,
          couponId: this.attributeForm.pid,
          number: this.attributeForm.total,
          cycleType: this.attributeForm.frequency
        }
        this.couponDtos.push(data)
        // this.list.push(this.attributeForm)
      } else if (this.attributeForm.discountType == 1) {
        if (this.attributeForm.subscribeId == undefined) {
          this.$notify.error({
            title: this.$t('Failed'),
            message: '请填选择商品'
          })
          return
        } else if (this.attributeForm.rules == undefined) {
          this.$notify.error({
            title: this.$t('Failed'),
            message: this.$t('请选择订阅规则')
          })
          return
        }
        for (let i = 0; i < this.ListSubscribe.length; i++) {
          if (this.ListSubscribe[i].id == this.attributeForm.subscribeId) {
            this.attributeForm.name = this.ListSubscribe[i].name
            this.attributeForm.total = 1
            this.attributeForm.discountType = 1
          }
        }
        var data = {
          subscribeId: this.attributeForm.subscribeId,
          subscribeRuleIds: [this.attributeForm.rules],
          subscribeName: this.attributeForm.name
        }
        this.subscribeVos.push(data)
        // this.list.push(this.attributeForm)
      } else {
        if (this.attributeForm.gid == null || this.attributeForm.gid == 'undefined') {
          this.$notify.error({
            title: this.$t('Failed'),
            message: this.$t('请填选择礼物卡')
          })
          return
        }

        for (let i = 0; i < this.listGiftCard.length; i++) {
          if (this.listGiftCard[i].id == this.attributeForm.gid) {
            this.attributeForm.name = this.listGiftCard[i].name
            this.attributeForm.total = 1
            this.attributeForm.discountType = 2
          }
        }

        var data = {
          name: this.attributeForm.name,
          giftCardId: this.attributeForm.gid,
          number: 1
        }
        this.giftCardVos.push(data)
      }
      this.attributeVisiable = false
      this.list.push(this.attributeForm)
    },
    handleDelete(data, row) {
      this.$confirm(this.$t('是否') + this.$t('删除该商品') + '?', this.$t('提示'), {
        confirmButtonText: this.$t('Confirm'),
        cancelButtonText: this.$t('Cancel'),
        type: 'warning'
      }).then(() => {
        ////debugger

        this.list.splice(data, 1)
        if (row.discountType == 1) {
          this.subscribeVos = this.subscribeVos.filter(item => item.name != row.name)
          this.ListSubscribe = this.ListSubscribe.filter(item => item.name != row.name)
        } else if (row.discountType == 0) {
          this.couponDtos = this.couponDtos.filter(item => item.name != row.name)
          this.listCoupon = this.listCoupon.filter(item => item.name != row.name)
        } else {
          this.giftCardVos = this.giftCardVos.filter(item => item.name != row.name)
          this.listGiftCard = this.giftCard.filter(item => item.name != row.name)
        }
      })
    },
    SelectChange(row) {
      for (var i = 0; i < this.ListSubscribe.length; i++) {
        if (this.ListSubscribe[i].id == row) {
          this.rule = this.ListSubscribe[i].rules
        }
      }
    },
    formateSubscribeRule(item) {
      let ruleStr = ''
      if (item.ruleType == 0) {
        ruleStr += this.$t('按天')
      } else if (item.ruleType == 1) {
        ruleStr += this.$t('按周')
      } else if (item.ruleType == 2) {
        ruleStr += this.$t('按月')
      } else if (item.ruleType == 3) {
        ruleStr += this.$t('按年')
      }
      if (item.daysTimes != 0 && item.daysTimes != undefined) {
        ruleStr += '(每天配送' + item.daysTimes + '次)'
      }
      if (item.weekTimes != 0 && item.weekTimes != undefined) {
        ruleStr += '(每周配送' + item.weekTimes + '次)'
      }
      if (item.monthTimes != 0 && item.monthTimes != undefined) {
        ruleStr += '(每月配送' + item.monthTimes + '次)'
      }
      if (item.yearTimes != 0 && item.yearTimes != undefined) {
        ruleStr += '(每年配送' + item.yearTimes + '次)'
      }
      return ruleStr
    },
    RadioChange(val) {
      if (!val) {
        this.checkList = [];
        this.dataForm.is_overlay = 0;
      }
    },

    getGoods(id) {
      this.listLoading = false
      return payMemberDelete(id).then(response => {
        this.couponDtos = response.data.data.couponDtos
        this.subscribeVos = response.data.data.subscribeVos
        this.giftCardVos = response.data.data.giftCardVos
        this.dataForm = response.data.data
        this.value1 = [this.dataForm.startTime, this.dataForm.endTime];
        this.checkList.push(this.dataForm.isOverlay);
        if (this.dataForm.buyTimes == 0) {
          this.buyTimeType = 0
          this.dataForm.buyTimes = 1
        } else if (this.dataForm.buyTimes == -1) {
          this.buyTimeType = 1
          this.dataForm.buyTimes = 1
        } else {
          this.buyTimeType = 2
        }
        if (this.dataForm.dicountPer) {
          this.dicountPer = 1
        }
        if (this.dataForm.shopIds) {
          this.areaLimit = 1
        }

        var giftCardVos = response.data.data.giftCardVos
        var couponDtos = response.data.data.couponDtos

        var subscribeVos = response.data.data.subscribeVos

        for (var i = 0; i < couponDtos.length; i++) {
          couponDtos[i].total = couponDtos[i].number
          for (var j = 0; j < this.listCoupon.length; j++) {
            if (couponDtos[i].couponId == this.coupon[j].id) {
              couponDtos[i].name = this.listCoupon[j].name
              couponDtos[i].discountType = 0
            }
          }
        }

        for (var i = 0; i < this.subscribeVos.length; i++) {
          for (var j = 0; j < this.ListSubscribe.length; j++) {
            if (subscribeVos[i].subscribeId == this.ListSubscribe[j].id) {

              subscribeVos[i].name = this.ListSubscribe[j].name
              subscribeVos[i].number = 1
              subscribeVos[i].total = 1
              subscribeVos[i].discountType = 1

            }
          }
        }

        // //debugger
        for (var i = 0; i < this.giftCardVos.length; i++) {
          for (var j = 0; j < this.listGiftCard.length; j++) {
            if (giftCardVos[i].giftCardId == this.listGiftCard[j].id) {
              giftCardVos[i].number = 1
              giftCardVos[i].name = this.listGiftCard[j].name
              giftCardVos[i].total = 1
              giftCardVos[i].discountType = 2
            }
          }
        }
        this.list = [...couponDtos, ...subscribeVos, ...giftCardVos]
        // console.log(this.dataForm)
      }).catch(() => {
      })
    },
    handleBack() {
      this.list = []
      this.$router.push({ path: '/user/PaidMemberSettings' })
    },
    updateData() {
      if (!this.dataForm.name) {
        this.$message.error('请填写会员名称')
        return
      } else if (!this.dataForm.picUrl) {
        this.$message.error('请上传图片')
        return
      }

      // else if (!this.dataForm.expiredDays) {
      //   this.$message.error('请填写有效期')
      //   return
      // }
      else if (!this.dataForm.money && !this.dataForm.point) {
        this.$message.error('请填写购买金额或积分')
        return
      } else if (this.dataForm.isSupportReneval && !this.dataForm.renevalMoney && !this.dataForm.renevalPoint) {
        this.$message.error('请填写续约购买金额或积分')
        return
      } else if (!this.dataForm.pointRatio) {
        this.$message.error('请填写积分获取比例')
        return
      }
      if (!(this.dataForm.startTime && this.dataForm.endTime) && !this.expiredDays) {
        this.$message.error('有效期不能为空')
        return;
      }
      if (this.buyTimeType == 0) {
        this.dataForm.buyTimes = 0
      } else if (this.buyTimeType == 1) {
        this.dataForm.buyTimes = -1
      }
      // if (this.dataForm.startTime && this.dataForm.endTime) {
      //     callback();
      //     return;
      //   }
      //   else if (this.days) {
      //     callback();
      //   }
      //   else {
      //     return callback(new Error(this.$t('有效期不能为空')));
      //   }
      var data = {
        name: this.dataForm.name,
        id: this.dataForm.id,
        picUrl: this.dataForm.picUrl,//
        useRange: this.dataForm.useRange,//
        // shopIds:this.dataForm.areaLimit,//
        expiredDays: this.dataForm.expiredDays,//
        buyTimes: this.dataForm.buyTimes,//
        money: this.dataForm.money,//
        point: this.dataForm.point,
        isSupportReneval: this.dataForm.isSupportReneval == true ? 1 : 0,//是否可以续购 0--不可以，1--可以
        shopIds: this.dataForm.shopIds,
        renevalMoney: this.dataForm.renevalMoney,
        renevalPoint: this.dataForm.renevalPoint,
        status: this.dataForm.status,
        pointRatio: this.dataForm.pointRatio,
        dicountPer: this.dicountPer == 0 ? undefined : parseInt(this.dataForm.dicountPer),
        paymentInstructions: this.dataForm.paymentInstructions,
        couponDtos: this.couponDtos.length <= 0 ? undefined : this.couponDtos,
        subscribeVos: this.subscribeVos.length <= 0 ? undefined : this.subscribeVos,
        giftCardVos: this.giftCardVos.length <= 0 ? undefined : this.giftCardVos,
        startTime: this.dataForm.startTime,
        endTime: this.dataForm.endTime,
        isOverlay: this.dataForm.is_overlay || 0,
      }
      // console.log(data);
      if (this.type == 2) {
        payMemberUpdate(data).then(res => {
          this.$notify.success({
            title: this.$t('Success!'),
            message: res.data.data
          })
          this.$router.push({ path: '/user/PaidMemberSettings' })
        }).catch(res => {
          this.$notify.error({
            title: this.$t('Failed'),
            message: res.data.errmsg
          })
        })
      } else {
        payMemberAdd(data).then(res => {
          this.$notify.success({
            title: this.$t('Success!'),
            message: res.data.data
          })
          this.$router.push({ path: '/user/PaidMemberSettings' })
        }).catch(res => {
          this.$notify.error({
            title: this.$t('Failed'),
            message: res.data.errmsg
          })
        })
      }

      this.list = []
    },
    uploadPicUrl: function (response) {
      this.dataForm.picUrl = response.data.url
    },
    handleGoodsPicRemove: function () {
      this.dataForm.picUrl = undefined
    },
    handleChangeType() {
      if (this.dataForm.type == 3) {
        this.dataForm.userLevel = [0]
        this.dataForm.promotionOnly = false
        this.dataForm.timeType = 1
        this.dataForm.goodsType = 0
      }
    },

    InputFoucs() {
    }
  },

  computed: {
    headers() {
      return {
        'X-Litemall-Admin-Token': getToken()
      }
    }
  }

}
</script>

<style scoped>
/deep/ input::-webkit-inner-spin-button,
/deep/ input::-webkit-outer-spin-button {
  -webkit-appearance: none !important;
}

/deep/ input[type="number"] {
  -moz-appearance: textfield;
}

.TipsTitle {
  line-height: 36px;
  width: 110px;
  text-align: left;
  vertical-align: middle;
  float: left;
  font-size: 14px;
  color: #606266;
}

.TopAdd {
  margin: 50px 0px 10px 0px;
  display: flex;
  justify-content: space-between;
}

.el-dialog__footer {
  text-align: center;
}

.avatar {
  width: 150px;
  height: 150px;
}

.el-upload {
  width: 150px;
  height: 150px;
  border: 1px dashed rgb(204, 204, 204);
  line-height: 150px;
}

.com {
  color: #606266;
  font-weight: 500;
  cursor: pointer;
  display: flex;
}
</style>
